/**
 * Created by dmrj on 2016/4/28.
 */
define(['jquery','appConst', 'appMessageBox'], function ($, appConstant, msgBox) {
    var email;
    return {
        runDataTables: function () {
            email = this;
            var customParams = function (params) {
                $.extend(params, {});
                params['draw'] = params.draw;
                return params;
            };
            var dt_emails = $('#dt_emails').dataTable({
                //"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'><'col-sm-6 col-xs-12 hidden-xs'>r>" +
                //"t" +
                //"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
                "processing":true,
                "pageLength": 100,
                "paging": false,
                "searching":false,
                //"pagingType":   "full_numbers",
                "autoWidth": false,
                "serverSide": true,
                "stateSave": true,
                "ordering": false,
                "language": appConstant.table_language(),
                "ajax": {
                    "url": 'system/email/list',
                    "type": "POST",
                    "dataSrc": "body",
                    "traditional": true,
                    "data": customParams,
                    "error": function (e) {
                        if (e.status == 401) {
                            msgBox.sessionFailureBox();
                        }
                        if (e.status == 500) {
                            msgBox.serverFailureBox();
                        }
                        if (e.status == 404) {
                            msgBox.pathFailureBox();
                        }
                    }
                },
                //"processing": true,
                //"deferRender": true,
                "columns": [
                    {"data": null},
                    {"data": "id"},
                    {"data": "name"},
                    {"data": null},
                    {"data": "status"},
                    {"data": null}
                ],
                "columnDefs": [
                    {
                        "targets": [1],
                        "visible":false
                    },
                    {
                        "targets": [3],
                        "render": function (data, type, full) {
                            var prefix = full.prefix == null ? '' : full.prefix,
                                username = full.username == null ? '' : full.username;

                            var detail = '<dl class="dl-horizontal">'
                                + '<dt>'+ $.i18n.prop('GLOBAL_FROM')+'</dt>'
                                +'<dd>' + full.sendAddress + '</dd>'
                                +'<dt>'+ $.i18n.prop('EMAIL_PREFIX')+'</dt>'
                                +'<dd>' + prefix + '</dd>'
                                +'<dt>'+ $.i18n.prop('EMAIL_HOST')+'</dt>'
                                +'<dd>' + full.hostAddress + '</dd>'
                                +'<dt>'+ $.i18n.prop('EMAIL_PORT')+'</dt>'
                                +'<dd>' + full.port + '</dd>'
                                +'<dt>'+ $.i18n.prop('EMAIL_USERNAME')+'</dt>'
                                +'<dd>' + username + '</dd>'
                                +'</dl>';
                            return detail;
                        }
                    },
                    {
                        "targets": [4],
                        "render": function (data, type, full) {
                            var status = '';
                            if (full.status == 'ACTIVE') {
                                status = '<span class="label label-success updateEmail_email" id="' + full.id + '_INACTIVE" style="cursor:pointer">'+ $.i18n.prop('EMAIL_STATUS_ACTIVE')+'</span>';
                            }
                            else if (full.status == 'INACTIVE') {
                                status = '<span class="label label-danger updateEmail_email" id="' + full.id + '_ACTIVE" style="cursor:pointer">'+ $.i18n.prop('EMAIL_STATUS_INACTIVE')+'</span>';
                            }
                            return status;
                        }
                    }, {
                        "targets": [5],
                        "render": function (data, type, full) {
                            //console.log(deleteEmailBox('2'));
                            var id = full.id;
                            var operate = '<div id="sssss" class="center">'
                                + '<a href="#system/email/edit/' + full.id + '" class="details-control" >'+ $.i18n.prop('GLOBAL_EDIT')+'</a>' + ' '
                                + '<a href="javascript:void(0); " class="deleteBox_email" id="' + id + '" >'+ $.i18n.prop('GLOBAL_DELETE')+'</a>' + ' '
                                + '<a href="javascript:void(0);" class="details-control testBox_email" id="' + id + '">'+ $.i18n.prop('EMAIL_TEST')+'</a>'
                                + '</div>';
                            return operate;
                        }
                    }
                ]
            });
            /*Deal with the index number of column 1;*/
            $('#dt_emails').DataTable().on('draw.dt', function () {
                var dt = $(this).dataTable();
                dt.api().column(0, {draw: 'applied'}).nodes().each(function (cell, i) {
                    //var pageNum = dt.api().page();
                    //var pageSize = dt.api().page.len();
                    cell.innerHTML = i + 1;
                });

                $("#dt_emails").find(".deleteBox_email").unbind('click').on('click', function (obj) {
                    email.deleteEmailBox($(this).attr("id"));
                });
                $("#dt_emails").find(".testBox_email").unbind('click').on('click', function (obj) {
                    email.testEmailBox($(this).attr("id"));
                });
                $("#dt_emails").find(".updateEmail_email").unbind('click').on('click', function (obj) {
                    var obj = $(this).attr("id").split("_");
                    email.updateEmailStatus(obj[0], obj[1]);
                });
            });
        },
        updateEmailStatus: function (emailId,status) {
            $.ajax({
                "url": "system/email/updateEmailStatus",
                "dataType" : "json",
                "data" :{'emailId':emailId,'status':status},
                "type": "POST",
                "success": function (d) {
                    if (d.code == 0) {
                        msgBox.errorBox(d.message != null ? d.message : 'GLOBAL_UPDATE_FAIL', 'MENU_SYSTEM_EMAIL');
                        return false;
                    } else {
                        msgBox.successBox({
                            content: $.i18n.prop('GLOBAL_UPDATE_SUCCESS')
                        });
                    }
                    $('#dt_emails').DataTable().draw();
                },
                "error": function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                }
            });
        },
        deleteEmailBox: function (id) {
            msgBox.show({
                title: $.i18n.prop('MESSAGEBOX_CONFIRM_TITLE'),
                content: $.i18n.prop('MESSAGEBOX_CONFIRM_DELETE_CONTENT', $.i18n.prop('MENU_SYSTEM_EMAIL')) + " <p class='text-align-right'>" +
                "<button type='submit' data-id="+id+" class='btn btn-primary btn-sm deleteEmail"+id+"'>"+$.i18n.prop('GLOBAL_BUTTON_YES')+"</button> " +
                "<a href='javascript:void(0);' class='btn btn-danger btn-sm'>"+$.i18n.prop('GLOBAL_BUTTON_NO')+"</a></p>",
                type: 'normal',
                timeout: false,
                clean: true
            });

            $('#divSmallBoxes').find(".deleteEmail" + id).unbind('click').on('click', function (e) {
                email.deleteEmail(id);
            });
        },
        deleteEmail: function (id) {
            $.ajax({
                "url": "system/email/delete/" + id,
                "type": "POST",
                "success": function (d) {
                    if (d.code == 0) {
                        msgBox.errorBox(d.message != null ? d.message : 'GLOBAL_UPDATE_FAIL', 'MENU_SYSTEM_EMAIL');
                        return false;
                    } else {
                        msgBox.successBox({
                            content: $.i18n.prop('GLOBAL_DELETE_SUCCESS', [$.i18n.prop('MENU_SYSTEM_EMAIL')])
                        });
                    }
                    $('#dt_emails').DataTable().draw();
                },
                "error": function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                }
            });
        },
        testEmailBox: function (id) {
            msgBox.show({
                title: $.i18n.prop('MESSAGEBOX_CONFIRM_TITLE'),
                content: $.i18n.prop('MESSAGEBOX_CONFIRM_TEST_CONTENT', $.i18n.prop('MENU_SYSTEM_EMAIL')) + " <p class='text-align-right'>" +
                "<button type='submit' data-id=" + id + " class='btn btn-primary btn-sm testEmail"+id+"'>"+$.i18n.prop('GLOBAL_BUTTON_YES')+"</button>" +
                "<a href='javascript:void(0);' class='btn btn-danger btn-sm'>"+$.i18n.prop('GLOBAL_BUTTON_NO')+"</a></p>",
                type: 'normal',
                timeout: false,
                clean: true
            });

            $('#divSmallBoxes').find(".testEmail" + id).unbind('click').on('click', function (e) {
                email.testEmail(id);
            });
        },
        testEmail: function (emailId) {
            $.ajax({
                "url": "system/email/testEmail/"+ emailId,
                "type": "POST",
                "success": function (d) {
                    if (d.code == 0) {
                        msgBox.errorBox(d.message, d.message != null ? 'MENU_SYSTEM_EMAIL' : 'EXCEPTION_EMAIL_FAIL');
                        return false;
                    } else {
                        msgBox.successBox({
                            content: $.i18n.prop('EXCEPTION_EMAIL_SUCCESS')
                        });
                    }
                },
                "error": function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                }
            });
        }
    }
});

